package com.heliang.dataaccess.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.heliang.dataaccess.bean.AdvBean;
import com.heliang.dataaccess.dao.AdvBeanDao;
import com.heliang.dataaccess.dao.BaseDao;
import com.heliang.dataaccess.db.DataBaseHelper;
import com.heliang.util.Constants;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.handle.ExceptionHandle;

public class AdvertisementDaoimpl extends BaseDao implements AdvBeanDao {

    public AdvertisementDaoimpl(Context context) {
        super(context);
    }

    /**
     * 插数据（顶部）
     * @param infoList
     * @return
     */
    public boolean saveTop(List<AdvBean> infoList) throws DaoException {
        try {
            if (infoList == null || infoList.size() <= 0) {
                return false;
            }

            for (AdvBean advBean : infoList) {
                advBean.setPosition(0);
                finalDb.save(advBean);
            }

            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "AdvertisementDaoimpl.saveTop()" + "保存顶部广告类别失败！");
            return false;
        }
    }

    /**
     * 删除数据(顶部)
     * @param idArray
     * @return
     */
    public boolean deleteTop(int[] idArray) throws DaoException {
        StringBuffer strWhere = new StringBuffer();

        try {
            if (idArray != null) {
                strWhere.append("ID IN(");
                for (int i = 0; i < idArray.length; i++) {
                    if (i != (idArray.length - 1)) {
                        strWhere.append(idArray[i] + ",");
                    } else {
                        strWhere.append(idArray[i]);
                    }
                }
                strWhere.append(" )  AND POSITION = 0 ");

                finalDb.deleteById(AdvBean.class, strWhere);
            }

            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "AdvertisementDaoimpl.deleteTop()" + "删除下发的推荐广告失败！");
            return false;
        }
    }

    /**
     * 查询数据(顶部)
     * @return
     */
    public List<AdvBean> queryTop() throws DaoException {
        List<AdvBean> advTopList = new ArrayList<AdvBean>();
        try {
            advTopList = finalDb.findAllByWhere(AdvBean.class, " POSITION = 0 ", " SORT_ID ");
            return advTopList;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "AdvertisementDaoimpl.queryTop()" + "查询顶部广告失败！");
            return null;
        }
    }

    /**
     * 插数据（底部）
     * @param infoList
     * @return
     */
    public boolean saveFoot(List<AdvBean> infoList) throws DaoException {
        try {
            if (infoList == null || infoList.size() <= 0) {
                return false;
            }

            for (AdvBean advFootBean : infoList) {
                advFootBean.setPosition(1);
                finalDb.save(advFootBean);
            }
            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "AdvertisementDaoimpl.saveFoot()" + "保存底部广告失败！");
            return false;
        }
    }

    /**
     * 删除数据(底部)
     * @param idArray
     * @return
     */
    public boolean deleteFoot(int[] idArray) throws DaoException {
        try {
            StringBuffer strWhere = new StringBuffer();
            if (idArray != null) {
                System.out.println(idArray.length);
                strWhere.append(" ID IN(");
                for (int i = 0; i < idArray.length; i++) {
                    if (i != (idArray.length - 1)) {
                        strWhere.append(idArray[i] + ",");
                    } else {
                        strWhere.append(idArray[i]);
                    }
                }
                strWhere.append(") AND POSITION = 1 ");

                finalDb.deleteByWhere(AdvBean.class, strWhere.toString());
            }

            return true;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_DELETE_ERROR_002, "AdvertisementDaoimpl.deleteFoot()" + "删除底部广告失败！");
            return false;
        }
    }

    /**
     * 查询数据(底部)
     * @return
     */
    public List<AdvBean> queryFoot() throws DaoException {
        List<AdvBean> advTopList = new ArrayList<AdvBean>();
        try {
            advTopList = finalDb.findAllByWhere(AdvBean.class, "  AND POSITION = 1 ", " SORT_ID ");
            return advTopList;
        } catch (Exception e) {
            ExceptionHandle.throwDaoException(e, ErrorCode.DAO_QUERY_ERROR_004, "AdvertisementDaoimpl.queryTop()" + "查询底部广告失败！");
            return null;
        }
    }

}
